from time import sleep  # 等待时间

import pyautogui  # 主力模块


# import xlrd  # 读取Excel文件数据
# import pyperclip  # 剪贴板功能


# 登录检查
def checkIn():
    print("游戏启动在前台了吗？（y/n）")
    answer = input("请输入：")
    while True:
        if answer == 'y':
            print("正在接入自动化管控...")
            try:
                location = pyautogui.locateCenterOnScreen(image='clickArea/miniGame.png', confidence=0.9)
            #     TODO:无法判断程序是否存在
            except:
                print("接管失败...请检查游戏界面是否置于首页")
            finally:
                print('接管成功...')
            break
        elif answer == 'n':
            print("  游戏后台启动中...（请不要遮挡游戏界面）")
            location = pyautogui.locateCenterOnScreen(image='clickArea/ico.png', confidence=0.3)
            # 此处为0.2，别的地方0.9没有问题
            # confidence参数即为查找精确度，默认为0.9更科学。找不到对象时会返回None
            if location is not None:
                pyautogui.click(location.x, location.y, clicks=2, interval=0.2, duration=0.2)
            else:
                print("  未找到程序入口，请将快捷方式放至在桌面上。")
                print("\t自动为您退出程序...")
                print("----------------------------------------")
                exit()
            # TODO:把这里封装成一个传参的方法
            while True:
                location = pyautogui.locateCenterOnScreen(image='clickArea/start.png', confidence=0.9)
                # confidence参数即为查找精确度，默认为0.9更科学。找不到对象时会返回None
                if location is not None:
                    pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
                    break
                sleep(1)
        else:
            print("输入的信息有误！请重新输入")
    print("游戏已就绪!")
    print("----------------------------------------")


# 卡Bug获取金币
def blank_1():
    # 需要的金币个数（传值，异常处理）
    # def needCoinNum():
    while True:
        coinNum = input("请输入你需要获取的金币个数（500的倍数）：")

        if coinNum.isdigit():
            if int(coinNum) % 500 != 0:
                print('输入的数据有误！请重新输入！\n')
                sleep(1)
            else:
                global runNumber
                runNumber = int(coinNum) / 500
                break
        else:
            print("要你输的是整数数字你不明白？给爷重新认真输")
            sleep(1)
    print("----------------------------------------")
    # 获取金币
    # def earn():
    print("已就绪，开始运行自动化...")
    sleep(1)
    print("\n脚本日志：")
    print("----------------------------------------")
    while True:
        location = pyautogui.locateCenterOnScreen(image="clickArea/miniGame.png", confidence=0.9)
        if location is not None:
            pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
            break
    steps = ['clickArea/game1.png', 'clickArea/continue.png',
             'clickArea/coin.png']
    # TODO:数组第一项不加入字典的范例
    for frequency in range(1, int(runNumber) + 1):
        for i in steps:
            while True:
                location = pyautogui.locateCenterOnScreen(image=i, confidence=0.9)
                # confidence参数即为查找精确度，默认为0.9更科学。找不到对象时会返回None
                if location is not None:
                    pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
                    break
                    # （interval=每次输入之间间隔0.2s；duration 持续时间；）
                sleep(1)
        print("\t日志记录————已自动拾取：" + str(frequency * 500) + "金币")
        print("\t运行次数————" + str(frequency) + "次")
        print("----------------------------------------")
    while True:
        location = pyautogui.locateCenterOnScreen(image="clickArea/miniGame_menu.png", confidence=0.9)
        # confidence参数即为查找精确度，默认为0.9更科学。找不到对象时会返回None
        if location is not None:
            pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
            break
            # （interval=每次输入之间间隔0.2s；duration 持续时间；）
        sleep(1)
    print("\t计划任务已完成，为您返回主菜单...")
    sleep(2)
    print("----------------------------------------")
    return menu()


# 花园自动打理
def blank_2():
    # def gardenCare():
    # 花园浇水
    # pyautogui.click((pyautogui.locateCenterOnScreen(image="clickArea/garden.png", confidence=0.9)).x,
    #                 (pyautogui.locateCenterOnScreen(image="clickArea/garden.png", confidence=0.9)).y)
    print("功能开发中，返回主菜单...")
    return menu()

    # 花园金币拾取
    # def coinPick():
    # # steps = ['clickArea/goldenCoin.png', 'clickArea/silverCoin.png']
    # steps = ['clickArea/silverCoin.png']
    # for c in steps:
    #     while True:
    #         location = pyautogui.locateCenterOnScreen(image=c, confidence=0.9)
    #         # confidence参数即为查找精确度，默认为0.9更科学。找不到对象时会返回None
    #         if location is not None:
    #             pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
    #             # （interval=每次输入之间间隔0.2s；duration 持续时间；）
    #         sleep(0.2)
    # print("----------------------------------------")
    # print("功能开发中，返回主菜单...")
    # return menu()


# 商店快速购买
def blank_3():
    print("功能开发中，返回主菜单...")
    return menu()


# 全自动定制游戏打卡
def blank_4():
    print("功能开发中，返回主菜单...")
    return menu()


# 帮助
def blank_5():
    print("功能开发中，返回主菜单...")
    return menu()


# 系统退出
def sys_exit():
    quit = ["clickArea/quit1.png", "clickArea/quit2.png"]
    for q in quit:
        location = pyautogui.locateCenterOnScreen(image=q, confidence=0.9)
        if location is not None:
            pyautogui.click(location.x, location.y, clicks=1, interval=0.2, duration=0.2)
            print("已为您退出程序...")
        else:
            print("无法执行退出操作，请检查相关信息...")
        pass
    print("再见")
    exit()
    # pyautogui.moveTo(0,0)
    # TODO:开发中...
    # 提前手动结束自动化、结束程序


# 菜单
def menu():
    print("\t主菜单：")
    print("\t1. 卡Bug获取金币")
    print("\t2. 花园自动打理")
    print("\t3. 商店快速购买")
    print("\t4. 全自动定制游戏打卡")
    print("\t5. 帮助")
    print("\t6. 退出")
    print("----------------------------------------")
    menu_choose = input("请输入你要使用的功能：")
    if menu_choose == "1":
        blank_1()
    if menu_choose == "2":
        blank_2()
    if menu_choose == "3":
        blank_3()
    if menu_choose == "4":
        blank_4()
    if menu_choose == "5":
        blank_5()
    if menu_choose == "6":
        sys_exit()


# 主类
if __name__ == '__main__':
    print('欢迎使用植物大战僵尸年度版小助手V1.3')
    print('  @GeekChunky    QQ:842093229')
    print("----------------------------------------")
    sleep(1)
    checkIn()
    # 进入菜单界面
    menu()

# TODO:脚本日志txt写入功能
# TODO:花园自动领金币小工具
# TODO:商店自动购买化肥、除虫剂小工具
